Token.js ➔ isAccessTokenValid   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
/**
2
3
 Copyright (c) 2018 Intuit
4
 #
5
 # Licensed under the Apache License, Version 2.0 (the "License");
6
 # you may not use this file except in compliance with the License.
7
 # You may obtain a copy of the License at
8
 #
9
 #  http://www.apache.org/licenses/LICENSE-2.0
10
 #
11
 # Unless required by applicable law or agreed to in writing, software
12
 # distributed under the License is distributed on an "AS IS" BASIS,
13
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 # See the License for the specific language governing permissions and
15
 # limitations under the License.
16
17
 */
18
19
/**
20
 * @namespace Token
21
 */
22
23
'use strict';
24
25
/**
26
 * @param {Cache} options.cache
27
 * @param {string} options.cacheId
28
 * @constructor
29
 * @property {Cache} _cache
30
 * @property {string} _cacheId
31
 */
32
function Token(params) {
33
  params = params || {};
34
35
  this.realmId = params.realmId || '';
36
  this.token_type = params.token_type || '';
37
  this.access_token = params.access_token || '';
38
  this.refresh_token = params.refresh_token || '';
39
  this.expires_in = params.expires_in || 0;
40
  this.x_refresh_token_expires_in = params.x_refresh_token_expires_in || 0;
41
  this.id_token = params.id_token || '';
42
  this.latency = params.latency || 60 * 1000;
43
  this.createdAt = params.createdAt || Date.now();
44
}
45
46
/**
47
 * get accessToken()
48
 * @returns {string} access_token
49
 */
50
Token.prototype.accessToken = function accessToken() {
51
  return this.getToken().access_token;
52
};
53
54
/**
55
 * get refreshToken()
56
 * @returns {string} refresh_token
57
 */
58
Token.prototype.refreshToken = function refreshToken() {
59
  return this.getToken().refresh_token;
60
};
61
62
/**
63
 * get tokenType()
64
 * @returns {string} token_type
65
 */
66
Token.prototype.tokenType = function tokenType() {
67
  return this.getToken().token_type;
68
};
69
70
71
/**
72
 * Helper Method to get accessToken { get Token Object }
73
 * @returns {{
74
 *  token_type: *,
75
 *  access_token: *,
76
 *  expires_in: *,
77
 *  refresh_token: *,
78
 *  x_refresh_token_expires_in: *
79
 * }}
80
 */
81
Token.prototype.getToken = function getToken() {
82
  return {
83
    token_type: this.token_type,
84
    access_token: this.access_token,
85
    expires_in: this.expires_in,
86
    refresh_token: this.refresh_token,
87
    x_refresh_token_expires_in: this.x_refresh_token_expires_in,
88
    realmId: this.realmId,
89
    id_token: this.id_token,
90
    createdAt: this.createdAt,
91
  };
92
};
93
94
/**
95
 * Helper Method to set accessToken { set Token Object }
96
 * @param tokenData
97
 * @returns {Token}
98
 */
99
Token.prototype.setToken = function setToken(tokenData) {
100
  this.access_token = tokenData.access_token;
101
  this.refresh_token = tokenData.refresh_token;
102
  this.token_type = tokenData.token_type;
103
  this.expires_in = tokenData.expires_in;
104
  this.x_refresh_token_expires_in = tokenData.x_refresh_token_expires_in;
105
  this.id_token = tokenData.id_token || '';
106
  this.createdAt = tokenData.createdAt || Date.now();
107
  return this;
108
};
109
110
/**
111
 * Helper Method to clear accessToken { clear Token Object }
112
 * @param
113
 * @returns {Token}
114
 */
115
Token.prototype.clearToken = function clearToken() {
116
  this.access_token = '';
117
  this.refresh_token = '';
118
  this.token_type = '';
119
  this.expires_in = 0;
120
  this.x_refresh_token_expires_in = 0;
121
  this.id_token = '';
122
  this.createdAt = 0;
123
  return this;
124
};
125
126
/**
127
 * Helper Method to check token expiry { set Token Object }
128
 * @param seconds
129
 * @returns {boolean}
130
 */
131
Token.prototype._checkExpiry = function _checkExpiry(seconds) {
132
  const expiry = this.createdAt + (seconds * 1000);
133
  return (expiry - this.latency > Date.now());
134
};
135
136
/**
137
 * Check if access_token is valid
138
 * @returns {boolean}
139
 */
140
Token.prototype.isAccessTokenValid = function isAccessTokenValid() {
141
  return this._checkExpiry(this.expires_in);
142
};
143
144
/**
145
 * Check if there is a valid (not expired) access token
146
 * @return {boolean}
147
 */
148
Token.prototype.isRefreshTokenValid = function isRefreshTokenValid() {
149
  return this._checkExpiry(this.x_refresh_token_expires_in);
150
};
151
152
module.exports = Token;
153